<template>
  <view class="container" style="padding-bottom: 100px" v-if="isLoaded">
    <view class="content" style="padding: 30rpx 30rpx 120rpx; background-color: #fff">
      <li-form>
        <li-form-item label="门店名称" required>
          <li-input v-model="form.storeName" rule="trim" prop="storeName" @blur="onBlurEvent($event)" />
        </li-form-item>
        <li-form-item label="门店联系人">
          <li-input v-model="form.linkMan" rule="trim" prop="linkMan" @blur="onBlurEvent($event)" />
        </li-form-item>
        <li-form-item label="联系电话">
          <li-input v-model="form.mobile" rule="trim" prop="mobile" @blur="onBlurEvent($event)" />
        </li-form-item>
        <li-form-item label="商户编号">
          <li-input v-model="form.payAccount" rule="trim" prop="payAccount" @blur="onBlurEvent($event)" />
        </li-form-item>
        <li-form-item label="所属区域" :border="false">
          <view style="background-color: #fff">
            <uni-data-picker :localdata="areaList" popup-title="请选择区域" @change="onchange" :value="formatAreaObj"></uni-data-picker>
          </view>
        </li-form-item>
        <li-form-item label="门店地址" required>
          <li-input v-model="form.address" rule="trim" prop="address" @blur="onBlurEvent($event)" />
        </li-form-item>
      </li-form>
    </view>

    <view
      class="bottom-btn"
      style="
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: 50px;
        background-color: #fdc31d;
        color: #fff;
        font-weight: bold;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 20px;
        letter-spacing: 3px;
      "
      v-on:click="btnEditOrSaveSubmitClick"
    >
      保存
    </view>
  </view>
</template>
<script>
import liFormMixin from '../../../mixins/liFormMixin';
import { DelStoreById, AddStore, GetStoreById, EditStore } from '../../../common/modules/m.js';

import { transferTypeEnum } from '../../../common/dataTransfer.js';
import { checkerType, modeEnum, booleanEnum, areaList } from '../../../constants/normalConstants.js';
import { corpId, storeId } from '../../../constants/apiConstants.js';
import { toastIconEnum } from '../../../constants/uiConstants.js';
import { uHideLoading, uShowLoading, utoast, uNavigateBack } from '@/utils/uniUtils';

export default {
  mixins: [liFormMixin],
  async onLoad(options) {
    this.loadEvent(options, this);
  },

  async mounted() {
    try {
      uShowLoading();
      await this.getData();
      this.isLoaded = true;
      const areaNameArr = this.form.areaFullName.split(' ');
      this.formatAreaObj =
        areaNameArr.length > 0
          ? [
              {
                text: areaNameArr[0],
                value: this.form.provinceId.toString()
              },
              {
                text: areaNameArr[1],
                value: this.form.cityId.toString()
              },
              {
                text: areaNameArr[2],
                value: this.form.areaId.toString()
              }
            ]
          : [
              {
                text: 0,
                value: '无'
              },
              {
                text: 0,
                value: '无'
              },
              {
                text: 0,
                value: '无'
              }
            ];
    } catch (error) {
      utoast('初始化失败~', toastIconEnum.error);
      setTimeout(() => {
        uNavigateBack();
      }, 800);
    } finally {
      uHideLoading();
    }
  },
  data() {
    return {
      areaList,
      modeEnum,
      booleanEnum,
      subject: '门店', // 主题
      rootRoute: 'workbench',
      routeName: 'store',
      mode: 'edit', // 模式
      isLoaded: false,
      id: null,
      /* 表单 */
      form: null,
      formatAreaObj: [],
      /* 弹窗 */

      // 数据转换器
      transferRules: [
        /*   {
            name: "depositMoney",
            type: transferTypeEnum.float,
          },
       */
      ],
      //表单校验规则
      rules: [
        {
          name: 'storeName',
          checkType: checkerType.endNotNull,
          errorMsg: '店铺名不能为空'
        },
        {
          name: 'address',
          checkType: checkerType.endNotNull,
          errorMsg: '地址不能为空'
        }
      ],
      lock: false, // 节流锁

      /* 额外数据 */
      DepartmentList: []
      /* 额外校验规则 */

      /* 额外转换器 */
    };
  },

  methods: {
    onBlurEvent(e) {
      this.formBlur(e, this);
    },

    async getData() {
      this.form = (await GetStoreById(this.id)).data;
    },

    async getOtherData() {},

    async insertData() {
      return AddStore(this.form);
    },

    async delData() {
      return await DelStoreById(this.id);
    },

    async editData() {
      return await EditStore(this.form);
    },

    async btnDelClick() {
      await this.formDelete(this);
    },

    async btnEditOrSaveSubmitClick() {
      await this.formInsertOrEdit(this);
    },
    onchange(e) {
      this.formatAreaObj = e.detail.value;
      let areaObj = [];
      if (this.formatAreaObj.length == 0) {
        areaObj = [0, 0, 0];
      } else {
        this.formatAreaObj.forEach((element) => {
          areaObj.push(element.value);
        });
      }
      this.form.areaObject = areaObj;
      this.form.provinceId = areaObj[0];
      this.form.cityId = areaObj[1];
      this.form.areaId = areaObj[2];
    }
    /* 额外方法放下面，上面是主干方法 */

    //方法放上面
  }
};
</script>
<style lang="scss" scoped>
@import '~@/static/styles/commonStyle.scss';
</style>
